package cn.imu.appdevexperiment2020._3activity.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import butterknife.BindView;
import cn.imu.appdevexperiment2020.R;
import cn.imu.appdevexperiment2020.common.BaseActivity;

/**
 * 描述:
 *
 * @author chenconghua
 * @version 1.0
 * @create 2022-11-13 18:32
 */
public class FragmentMainActivity extends BaseActivity implements View.OnClickListener {
   @BindView(R.id.main_menu_home)
   LinearLayout mainMenuHome;
   @BindView(R.id.main_menu_)
   LinearLayout mainMenu;
   @BindView(R.id.main_menu_my)
   LinearLayout mainMenuMy;
   Boolean isExit = false;

   String versionName;
   private Fragment fgHome, fgClient, fgMy;
   private FragmentManager manager;
   private FragmentTransaction transaction;
   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_fragment_main);
      manager = FragmentMainActivity.this.getSupportFragmentManager();
      transaction = manager.beginTransaction();
      initView();
   }
   private void initView() {
      mainMenuHome.setOnClickListener(this);
      mainMenu.setOnClickListener(this);
      mainMenuMy.setOnClickListener(this);
      mainMenuHome.setSelected(true);

      fgClient = new LeftFragment();
      fgMy = new MyFragment();


      fgHome = new HomeFragment();

      transaction.add(R.id.fg_content, fgClient);
      transaction.add(R.id.fg_content, fgMy);
      transaction.add(R.id.fg_content, fgHome);
      transaction.hide(fgClient);
      transaction.hide(fgMy);
      transaction.commit();

   }

   @Override
   public void onClick(View v) {

      manager = FragmentMainActivity.this.getSupportFragmentManager();
      transaction = manager.beginTransaction();
      switch (v.getId()) {
         case R.id.main_menu_home:
            hideFragment(transaction);
            transaction.show(fgHome);
            transaction.commit();
            mainMenuHome.setSelected(true);
            mainMenu.setSelected(false);
            mainMenuMy.setSelected(false);
            break;
         case R.id.main_menu_:
            hideFragment(transaction);


            transaction.show(fgClient);
            transaction.commit();
            mainMenuHome.setSelected(false);
            mainMenu.setSelected(true);
            mainMenuMy.setSelected(false);
            break;
         case R.id.main_menu_my:
            hideFragment(transaction);

            transaction.show(fgMy);
            transaction.commit();
            mainMenuHome.setSelected(false);
            mainMenu.setSelected(false);
            mainMenuMy.setSelected(true);
            break;

      }
   }
   private void hideFragment(FragmentTransaction transaction) {
      if (fgHome != null) {
         transaction.hide(fgHome);
      }
      if (fgClient != null) {
         transaction.hide(fgClient);
      }
      if (fgMy != null) {
         //transaction.hide(f3);


         transaction.hide(fgMy);
      }
   }
}
